package tech.fangsong.client.strategy;

import io.netty.channel.ChannelHandlerContext;
import tech.fangsong.im.core.protocol.ImMsg;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class MessageStrategyContext {

    private static MessageStrategyContext context = null;

    private List<MessageStrategy> strategyList = new CopyOnWriteArrayList<>();

    private MessageStrategyContext(){
    }

    public static MessageStrategyContext getInstance(){
        if(context==null){
            synchronized (MessageStrategyContext.class){
                if(context==null){
                    context = new MessageStrategyContext();
                    MessageStrategy singleChatMessageStrategy = new SingleChatMessageStrategy();
                    context.strategyList.add(singleChatMessageStrategy);
                }
            }
        }
        return context;
    }

    public ImMsg received(ChannelHandlerContext channelHandlerContext,ImMsg msg){
        for(MessageStrategy strategy:strategyList){
            if(strategy.support(msg)){
                return strategy.received(channelHandlerContext,msg);
            }
        }
        return null;
    }

}
